//
//  ViewController.m
//  Tomcat
//
//  Created by 宋 on 16/3/10.
//  Copyright © 2016年 宋. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *Imagecat;

- (IBAction)Eat;

- (IBAction)Drink;

- (IBAction)Fart;

- (IBAction)Cymbal;

- (IBAction)Knockout;

- (IBAction)Pie;

- (IBAction)Scratch;

- (IBAction)FootR;

- (IBAction)FootL;

- (IBAction)Stomach;

- (IBAction)Angry;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
}
-(void)action:(int)count picname:(NSString *)picname{
    //判断当前动画是否正在执行，如果正在运行，点击其他按钮无效
    if (self.Imagecat.isAnimating == 1) {
        return;
    }
    //1.加载图片到帧动画数组
    NSMutableArray *arrayM = [[NSMutableArray alloc] init];
    //[arrayM addObject:@"eat_00.jpg"];
    for (int i = 0; i<count; i++) {
        //拼接图片名字
        NSString * imagename = [NSString stringWithFormat:@"%@_%02d.jpg",picname,i];
        //根据图片名字得到图片
        //UIImage *imgcat = [UIImage imageNamed:imagename];
        NSString *path = [[NSBundle mainBundle]pathForResource:imagename ofType:nil];
        UIImage *imgcat = [UIImage imageWithContentsOfFile:path];
        
        
        //将每次获取的图片放到数组中去
        [arrayM addObject:imgcat];
    }
    //2.将得到的帧动画数组赋给UIImageView的帧动画数组属性
    self.Imagecat.animationImages = arrayM;
    
    //3.设置动画播放时间
    self.Imagecat.animationDuration = self.Imagecat.animationImages.count * 0.1;
    
    //4.播放次数
    self.Imagecat.animationRepeatCount = 1;
    
    //5.开始播放帧动画
    [self.Imagecat startAnimating];
    
    //6.动画播放完，内存清空
    //self.Imagecat.animationImages = nil;
    [self.Imagecat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.Imagecat.animationImages.count * 0.1];

    
}
- (IBAction)Eat{
    [self action:39 picname:@"eat"];
}

- (IBAction)Drink{
    [self action:80 picname:@"drink"];
}

- (IBAction)Fart{
    [self action:27 picname:@"fart"];
}

- (IBAction)Cymbal{
    [self action:12 picname:@"cymbal"];
}

- (IBAction)Knockout{
    [self action:80 picname:@"knockout"];
}

- (IBAction)Pie {
    [self action:23 picname:@"pie"];
}

- (IBAction)Scratch {
    [self action:55 picname:@"scratch"];
}

- (IBAction)FootL {
    [self action:29 picname:@"foot_right"];
}

- (IBAction)Stomach {
    [self action:33 picname:@"stomach"];
}

- (IBAction)Angry {
    [self action:23 picname:@"angry"];
}

- (IBAction)FootR {
    [self action:29 picname:@"foot_left"];
}


/*- (IBAction)Eat {
   //1.加载图片到帧动画数组
    NSMutableArray *arrayM = [[NSMutableArray alloc] init];
    //[arrayM addObject:@"eat_00.jpg"];
    for (int i = 0; i<39; i++) {
        //拼接图片名字
        NSString * imagename = [NSString stringWithFormat:@"eat_%02d.jpg",i];
        //根据图片名字得到图片
        UIImage *imgcat = [UIImage imageNamed:imagename];
        //将每次获取的图片放到数组中去
        [arrayM addObject:imgcat];
    }
    //2.将得到的帧动画数组赋给UIImageView的帧动画数组属性
    self.Imagecat.animationImages = arrayM;
    
    //3.设置动画播放时间
    self.Imagecat.animationDuration = self.Imagecat.animationImages.count * 0.1;
    
    //4.播放次数
    self.Imagecat.animationRepeatCount = 1;
    
    //5.开始播放帧动画
    [self.Imagecat startAnimating];
}

- (IBAction)Drink {
    NSMutableArray *arrayM = [[NSMutableArray alloc]init];
    for (int i = 0; i < 80; i++) {
        
        NSString *imagename = [NSString stringWithFormat:@"drink_%02d.jpg",i];
        UIImage *imaged = [UIImage imageNamed:imagename];
        [arrayM addObject:imaged];
    }
    self.Imagecat.animationImages = arrayM;
    self.Imagecat.animationDuration = self.Imagecat.animationImages.count * 0.1;
    self.Imagecat.animationRepeatCount = 1;
    [self.Imagecat startAnimating];
}

- (IBAction)Fart {
    NSMutableArray *arrayM = [[NSMutableArray alloc]init];
    for (int i = 0; i < 27; i++) {
        NSString *imagename = [NSString stringWithFormat:@"fart_%02d.jpg",i];
        UIImage *imaged = [UIImage imageNamed:imagename];
        [arrayM addObject:imaged];
    }
    self.Imagecat.animationImages = arrayM;
    self.Imagecat.animationDuration = self.Imagecat.animationImages.count * 0.1;
    self.Imagecat.animationRepeatCount = 1;
    [self.Imagecat startAnimating];
}

- (IBAction)Cymbal {
    NSMutableArray *arrayM = [[NSMutableArray alloc]init];
    for (int i = 0; i < 12; i++) {
        NSString *imagename = [NSString stringWithFormat:@"cymbal_%02d.jpg",i];
        UIImage *imaged = [UIImage imageNamed:imagename];
        [arrayM addObject:imaged];
    }
    self.Imagecat.animationImages = arrayM;
    self.Imagecat.animationDuration = self.Imagecat.animationImages.count * 0.1;
    self.Imagecat.animationRepeatCount = 1;
    [self.Imagecat startAnimating];
}

- (IBAction)Knockout {
    NSMutableArray *arrayM = [[NSMutableArray alloc]init];
    for (int i = 0; i < 80; i++) {
        NSString *imagename = [NSString stringWithFormat:@"knockout_%02d.jpg",i];
        UIImage *imaged = [UIImage imageNamed:imagename];
        [arrayM addObject:imaged];
    }
    self.Imagecat.animationImages = arrayM;
    self.Imagecat.animationDuration = self.Imagecat.animationImages.count * 0.1;
    self.Imagecat.animationRepeatCount = 1;
    [self.Imagecat startAnimating];
}*/


@end
